package com.ewei.support.mail.impl;

import java.util.List;

import com.ewei.support.mail.MailReceiverFilter;
import com.ewei.support.mail.MailReceiverFilterChain;
import com.ewei.support.mail.javax.ReceiveMail;

/**
 * @author li
 * @version 1 (2016年1月16日 下午4:39:13)
 * @since Java7
 */
public abstract class MailReceiverFilterChainImpl implements MailReceiverFilterChain {
    private ReceiveMail receiveMail;

    private List<MailReceiverFilter> mailReceiverFilters;

    private int index = 0;

    public MailReceiverFilterChainImpl(ReceiveMail receiveMail, List<MailReceiverFilter> mailReceiverFilters) {
        this.receiveMail = receiveMail;
        this.mailReceiverFilters = mailReceiverFilters;
    }

    public List<MailReceiverFilter> getMailReceiverFilters() {
        return mailReceiverFilters;
    }

    public ReceiveMail getReceiveMail() {
        return this.receiveMail;
    }

    public void doFilter() {
        if (null == mailReceiverFilters || index >= mailReceiverFilters.size()) {// 如果没有AopFilter或者已经经过全部AopFilter
            invoke();// 执行目标方法
        } else {// 还有AopFilter
            mailReceiverFilters.get(index++).doFilter(this);// 执行第index个AopFilter然后index++
        }
    }
}